//
//  ReceiveCouponViewController.m
//  com.modada.new
//
//  Created by ferver7 on 2019/5/31.
//  Copyright © 2019 ferver7. All rights reserved.
//

#import "ReceiveCouponViewController.h"
#import "MyCouponModel.h"
#import "MyCouponListCell.h"
@interface ReceiveCouponViewController ()
@property (weak, nonatomic) IBOutlet UITableView *selfTableView;

@property (nonatomic,strong) NSMutableArray *dataArr;
@end

@implementation ReceiveCouponViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self customNavigationItems];
    mNavitationTitle(@"优惠券");
    self.dataArr = [[NSMutableArray alloc]init];
    [self loadMyCouponList];
}

#pragma mark -
#pragma mark - **************** tableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArr.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyCouponListCell * itemCell = mGetTableViewCell(@"myCouponListCell");
    MyCouponModel * itemModel = self.dataArr[indexPath.section];
    itemCell.priceLabel.text = [NSString stringWithFormat:@"%@",itemModel.money];
    itemCell.nameLabel.text = itemModel.couponName;
    itemCell.typeName = @"0";
    itemCell.timeLabel.text = [NSString stringWithFormat:@"%@-%@",itemModel.yxqq,itemModel.yxqz];
    if (itemModel.full.integerValue == 0) {
        itemCell.tiaoJianLabel.text = @"无限制";
    }else{
        itemCell.tiaoJianLabel.text = [NSString stringWithFormat:@"满%@元使用",itemModel.full];
    }
    itemCell.btnActionBlock = ^{
        NSMutableDictionary * parameters = [[NSMutableDictionary alloc]init];
        setDickeyobj(parameters, @"1", @"add");
        setDickeyobj(parameters, itemModel.cid, @"couponId");
        [PPNetworkHelper POST:app_coupon_getCoupon parameters:parameters success:^(id responseObject) {
            if (YJPPSUCCESS(responseObject)) {
                [self.dataArr removeObject:itemModel];
                [AxcUI_Toast AxcUI_showCenterWithText:@"领取成功"];
                [self.selfTableView reloadData];
            }else{
                
            }
            [self.selfTableView reloadData];
        } failure:^(NSError *error) {
            
        } showLoading:self.view];
    };
    return itemCell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 140;
}
-(void)loadMyCouponList
{
    NSMutableDictionary * parameters = [[NSMutableDictionary alloc]init];
    setDickeyobj(parameters, @"1", @"add");
    [PPNetworkHelper POST:app_coupon_hasReceiveCoupon parameters:parameters success:^(id responseObject) {
        if (YJPPSUCCESS(responseObject)) {
            for (NSDictionary * infoDic in responseObject[@"data"]) {
                MyCouponModel * itemModel = [[MyCouponModel alloc]init];
                [itemModel setValuesForKeysWithDictionary:infoDic];
                [self.dataArr addObject:itemModel];
            }
        }else{
            
        }
        [self.selfTableView reloadData];
    } failure:^(NSError *error) {
        
    } showLoading:nil];
}

@end
